package com.qkl.xiatang.app.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.regions.ProductDomain;
import com.qkl.xiatang.app.dto.sms.SmsSendRequest;
import com.qkl.xiatang.app.dto.sms.SmsSendResponse;
import com.qkl.xiatang.app.entity.sms.SmsDynamicConfig;
import lombok.extern.slf4j.Slf4j;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class SmsSendUtils {

    public static final Integer STATUS_SEND = 0;

    public static final Integer STATUS_ERROR = -1;

    public static final Integer STATUS_OK = 1;

    private static SecureRandom RANDOM = new SecureRandom();

    private SmsSendUtils(){

    }

    public static List<Integer> allStatus(){
        List<Integer> list = new ArrayList<>();
        list.add(STATUS_SEND);
        list.add(STATUS_ERROR);
        list.add(STATUS_OK);
        return list;
    }

    /**
     * 发送手机动态码
     * @param smsSendRequest
     * @return
     */
    public static SmsSendResponse sendPhoneCode(SmsSendRequest smsSendRequest){
        String regionId = smsSendRequest.getSmsConfig().getRegionId();
        String accesKeyId = smsSendRequest.getSmsConfig().getAccessKey();
        String verion = smsSendRequest.getSmsConfig().getVersion();
        String secret = smsSendRequest.getSmsConfig().getSecret();
        String domain = smsSendRequest.getSmsConfig().getDomain();
        String phoneNum =smsSendRequest.getPhoneNum();
        String singName = smsSendRequest.getSmsConfig().getSignName();
        String templateCode = smsSendRequest.getSmsConfig().getTemplateCode();
        String code = smsSendRequest.getCode();
        DefaultProfile profile = DefaultProfile.getProfile(regionId,accesKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setVersion(verion);
        request.setSysProductDomain(new ProductDomain("Dysmsapi",domain));
        request.setActionName("SendSms");
        request.setSysRegionId(regionId);
        request.setPhoneNumbers(phoneNum);
        request.setSignName(singName);
        request.setTemplateCode(templateCode);//SMS_223585140
        request.setTemplateParam("{\"code\":\"" + code + "\"}");

        SmsSendResponse response = new SmsSendResponse();
        try {
            //{"bizId":"115404899859021111^0","code":"OK","message":"OK","requestId":"97C2B84B-6C94-5083-ABAB-DF2A22BBA974"}
            SendSmsResponse resp = client.getAcsResponse(request);
            response.setCode(resp.getCode());
            response.setMessage(resp.getMessage());
            response.setBizId(resp.getBizId());
            response.setRequestId(resp.getRequestId());
        }catch (ClientException e) {
            e.printStackTrace();
            response.setMessage(e.getMessage());
        }
        log.info("sendPhoneCode response={}",response);
        return response;
    }

    public static SmsSendResponse sendPhoneCode(SmsDynamicConfig smsConfig, String phoneNum, String code){
        SmsSendRequest smsSendRequest = new SmsSendRequest();
        smsSendRequest.setCode(code);
        smsSendRequest.setSmsConfig(smsConfig);
        smsSendRequest.setPhoneNum(phoneNum);
        return sendPhoneCode(smsSendRequest);
    }

    /**
     * 生成验证码
     * @return
     */
    public static String genVerifyCode(){
        return String.valueOf(RANDOM.nextInt(9000) + 1000);
    }

    public static void main(String[] args) {
        for(int i = 0 ; i< 10000;i++) {
            String verifyCode = genVerifyCode();
            int len = verifyCode.length();
            if( len != 4) {
                System.out.println(verifyCode);
            }
        }
    }

}
